/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.search.impl;

import java.util.List;

import org.grayrabbit.cms.dao.search.KeywordsDao;
import org.grayrabbit.cms.dao.search.KeywordsRecommendDao;
import org.grayrabbit.cms.entity.Keywords;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.service.search.KeywordsService;
import org.grayrabbit.cms.vo.search.KeywordsBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-10-26 13:42:09  
 */
@Service
@Transactional
public class KeywordsServiceImpl extends GenericServiceImpl implements KeywordsService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("keywordsDaoImpl")
	private KeywordsDao keywordsDao;
	
	@Autowired
	@Qualifier("keywordsRecommendDaoImpl")
	private KeywordsRecommendDao keywordsRecommendDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see KeywordsService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public Keywords getKeywords(final String keywordsId){
		Keywords keywords = keywordsDao.findById(keywordsId);
		keywordsDao.initialize(keywords);
		return keywords;
	}
	
	/**
	 * @see KeywordsService#delete(String)
	 */
	@Transactional
	public void delete(final Keywords keywords) {
		synchronized (KeywordsContext.class) {
			keywordsDao.makeTransient(keywords);
			KeywordsContext.delete(keywords.getKeywordsId());
		}
		
	}
	
	/**
	 * @see KeywordsService#save(OperLog)
	 */
	@Transactional
	public void save(final Keywords keywords) {
		synchronized (KeywordsContext.class) {
			keywordsDao.makePersistent(keywords);
			KeywordsContext.put(keywords);
		}
			
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see KeywordsService#getPage(KeywordsBean)
	 */
	@Transactional(readOnly=true)
	public Page<Keywords> getPage(final KeywordsBean keywordsBean){
		Page<Keywords> page =  new Page<Keywords>();
		page.setTotal(keywordsDao.count(keywordsBean));
		page.setResult(keywordsDao.findByQc(keywordsDao.assembly(keywordsBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see KeywordsService#list(Page, KeywordsBean)
	 */
	public void list(Page<Keywords> page, KeywordsBean keywordsBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(keywordsDao.count(keywordsBean));
		}
		page.setResult(keywordsDao.findByPage(page,keywordsBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see KeywordsService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			keywordsRecommendDao.deleteByKewordsId(id);
			keywordsDao.makeTransient(keywordsDao.findById(id));
			KeywordsContext.delete(id);
			i++;
			if(i==MAX_CLEAR)
				keywordsDao.clear();
		}
		
	}
 
	public boolean isExistWords(String id, String word){
		return keywordsDao.isExistWords(id, word);
	}
	
	
	/**
	 * 内存化
	 */
	public void fillContext(){
		if(KeywordsContext.isUpdate()){
			synchronized (KeywordsContext.class) {
				KeywordsContext.UPDATE_FLAG = false;
				KeywordsContext.remove();
				List<Keywords> keywordsList = keywordsDao.findAll();
				for(Keywords k:keywordsList){
					KeywordsContext.put(k);
				}
			}
		}
	}
	
	public List<Keywords> findAll(){
		return keywordsDao.findAll();
	}
}